package reputation.persist;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import recomendation.Tag;
import reputation.ItemFile;

public class ItemFileDAO {
public static Connection conn = null; 
	
	public static void connect(){

		 conn = null;

        try
        {
            String userName = "root";
            String password = "";
            String url = "jdbc:mysql://localhost/ria";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
 	}
	
	public static void disconnect(){
		if (conn != null)
	    {
	        try
	        {
	        	conn.close ();
	            System.out.println ("Database connection terminated");
	        }
	        catch (Exception e) {  }
	    }
	}
	
	public static int getScoreByText(String file){
		
		int score=0;
		PreparedStatement s;
		ResultSet rs=null;
		String query="SELECT * from reputation WHERE text= '"+ file +"'";
		System.out.println(query);
		  try {  
			  if(conn!=null){
				  s = conn.prepareStatement (query);
				  //s.setString (1, file);
				 
				  rs = s.executeQuery ();
				  
				  if (rs.next()){
					 
					  score= rs.getInt("score");
					  System.out.println(score);
				  }
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return score;
	} 
	
	public static boolean insert(ItemFile ifl){
		 PreparedStatement s;
		 int count=0;
		 String query="INSERT INTO reputation (text, score) VALUES('"+ifl.getText() +"',"+ifl.getScore()+")";
		  try {  
			  if(conn!=null){
				  System.out.println(query);
				  s = conn.prepareStatement (query);
				  //s.setString (1, ifl.getText());
				  //s.setInt(2,ifl.getScore());
				  count = s.executeUpdate ();
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		   
		return count!=0?true:false;
	}
	
	public static boolean updateItemFile(ItemFile t){
		boolean b= udpdate(t);
		
		if (!b){
			t.setScore(1);
			return insert(t);
		}
		else{
			return b;
		}

		
	}
	
	public static boolean udpdate(ItemFile f){
		PreparedStatement s;
		ResultSet rs=null;
		int count=0;
		int score=0;
		String query="SELECT * from reputation WHERE text='"+ f.getText()+"'";
		  try {  
			  if(conn!=null){
				  s = conn.prepareStatement (query);
				  //s.setString (1, f.getText());
				 
				  rs = s.executeQuery ();
				  
				  if (rs.next()){
					  
					  score= (int)((rs.getInt("score")+f.getScore())/2);
					  
					  
					  query="UPDATE reputation SET score="+score+" WHERE text='"+f.getText()+"'";
					  s = conn.prepareStatement (query);
					  //s.setInt(1, score);
					  //s.setString (2, f.getText());
					  
					  count = s.executeUpdate ();
				  }
			  }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		   
		return count!=0?true:false;
	}

	
	
}
